package cn.kgc.ssm.service.impl;

import cn.hutool.crypto.SecureUtil;
import cn.kgc.ssm.dto.QueryUser;
import cn.kgc.ssm.entity.User;
import cn.kgc.ssm.enums.CodeMsg;
import cn.kgc.ssm.form.UserLoginForm;
import cn.kgc.ssm.mapper.UserMapper;
import cn.kgc.ssm.service.UserService;
import cn.kgc.ssm.util.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Objects;

/**
 * @author 课工场
 * @date 2023/10/19
 * @description
 */
@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;

    @Override
    public ResponseResult registerUser(User user) {

        // 注册用户信息之前   应该判定用户名是否已经被占用
        String username = user.getUsername();
        User usr =  userMapper.selectUserByName(username);

        if (Objects.isNull(usr)){
            //用户密码加密
            String password = user.getPassword();
            user.setPassword(SecureUtil.md5(password));
            userMapper.registerUser(user);
            return ResponseResult.success("注册成功",0);
        }

        return ResponseResult.success("用户名已存在",110);
    }


    @Override
    public ResponseResult login(UserLoginForm loginForm, HttpSession session) {

        // 判定验证码
        String code = loginForm.getCode();
        String sessionCode = (String) session.getAttribute("sessionCode");

        if (!sessionCode.equals(code)){
            return  ResponseResult.success(CodeMsg.CODE_ERROR);
        }
        // 判定用户名
        String username = loginForm.getUsername();
        User user = userMapper.selectUserByName(username);
        if (Objects.isNull(user)){
            return  ResponseResult.success(CodeMsg.USER_NOT_EXIST);
        }

        // 判定密码
        String password = user.getPassword();   // 密文
        String pwd = SecureUtil.md5(loginForm.getPassword()) ;  //  明文

        if (!password.equals(pwd)){
            return  ResponseResult.success(CodeMsg.PASSWORD_ERROR);
        }

        //  将验证通过的用户信息 保存在session中 用于后续的操作  权限

        session.setAttribute("user",user);

        return ResponseResult.success("登录成功",0);
    }

    @Override
    public ResponseResult getUserByName(String username) {

        User user = userMapper.selectUserByName(username);

        return ResponseResult.success(user,null);
    }

    @Override
    public ResponseResult editInfo(User user) {

        userMapper.editInfo(user);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult changePwd(String oldPwd, String newPassword, HttpSession session) {

        // 1.判定用户的原始密码是否正确
        User user = (User) session.getAttribute("user");
        String username = user.getUsername();
        User usr = userMapper.selectUserByName(username);

        String password = usr.getPassword();
        String md5Pwd = SecureUtil.md5(oldPwd);
        if (!password.equals(md5Pwd)){
            return  ResponseResult.success("原始密码不正确",114);
        }

        //  2. 根据用户的id  修改密码
        userMapper.changePwd(SecureUtil.md5(newPassword),usr.getId());

        return ResponseResult.success();
    }


    @Override
    public ResponseResult userPage(QueryUser userPageDTO) {

        PageHelper.startPage(userPageDTO.getPage(), userPageDTO.getLimit());

        //  获取搜索数据
        String city = userPageDTO.getCity();
        String username = userPageDTO.getUsername();

        List<User> list = userMapper.getList(username,city);

        PageInfo<User> userPageInfo = new PageInfo<>(list);

        // total  分页数据的总条数
        long total = userPageInfo.getTotal();
        // 当前页数据
        List<User> users = userPageInfo.getList();

        return ResponseResult.success(users,total);
    }

    @Override
    public ResponseResult addUser(User user) {

        //  1. 判定用户名是否已经存在

        String username = user.getUsername();
        User usr = userMapper.selectUserByName(username);

        if (!Objects.isNull(usr)){
            return  ResponseResult.success(CodeMsg.USER_EXIST);
        }

        // 2. 给用户设置默认密码   888888   md5加密
        user.setPassword(SecureUtil.md5("888888"));

        // 3.将用户信息写入数据库

        userMapper.addUser(user);

        return ResponseResult.success();
    }

    @Override
    public ResponseResult batchDelete(Integer[] ids) {

        userMapper.batchDelete(ids);

        return ResponseResult.success();
    }
}
